//
//  SendViewController.m
//  Before
//
//  Created by lanou3g on 15/11/30.
//  Copyright © 2015年 sss. All rights reserved.
//

#import "SendViewController.h"
#import "AVOSCloud.h"
#import "mapViewController.h"
@interface SendViewController ()<UIImagePickerControllerDelegate>
@property (nonatomic,strong)UITextView * textView;
@property (nonatomic,strong)UIImageView * imageView;
@property (nonatomic,strong)UIImageView * secondView;
@property (nonatomic,strong)UIImageView * thirdView;
@property (nonatomic,strong)UIImageView * fourthView;
@property (nonatomic,strong)UIButton * button;
@property (nonatomic,strong)AVObject * myObject;
@property (nonatomic,strong)mapViewController * map;
@end

@implementation SendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeData];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.textView becomeFirstResponder];
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.myObject = [AVObject objectWithClassName:@"Post"];
   
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.textView resignFirstResponder];
}



- (void)makeData
{
    self.textView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.textView];
    //第一张 图片
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 220, 100, 100)];
    self.imageView.backgroundColor = [UIColor grayColor];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    self.imageView.userInteractionEnabled = YES;
    [self.imageView addGestureRecognizer:tap];
    [self.view addSubview:self.imageView];
    // 第二张 图片
    self.secondView = [[UIImageView alloc] initWithFrame:CGRectMake(120, 220, 100, 100)];
    self.secondView.backgroundColor = [UIColor grayColor];
    self.secondView.hidden = YES;
    UITapGestureRecognizer * secondtap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    self.secondView.userInteractionEnabled = YES;
    [self.secondView addGestureRecognizer:secondtap];
    [self.view addSubview:self.secondView];
    // 第三张 图片
    self.thirdView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 220, 100, 100)];
    self.thirdView.backgroundColor = [UIColor grayColor];
    self.thirdView.hidden = YES;
    UITapGestureRecognizer * thirdtap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    self.thirdView.userInteractionEnabled = YES;
    [self.thirdView addGestureRecognizer:thirdtap];
    [self.view addSubview:self.thirdView];
    // 第四张 图片
    self.fourthView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 330, 100, 100)];
    self.fourthView.backgroundColor = [UIColor grayColor];
    self.fourthView.hidden = YES;
    UITapGestureRecognizer * fourthtap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    self.fourthView.userInteractionEnabled = YES;
    [self.fourthView addGestureRecognizer:fourthtap];
    [self.view addSubview:self.fourthView];


    
    
    // 发送按钮
    self.button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.button.backgroundColor = [UIColor orangeColor];
    [self.button setTitle:@"发送" forState:(UIControlStateNormal)];
    [self.view addSubview:self.button];
  
}
- (void)buttonAction:(id)sender
{
    // 创建评论内容
    [self.myObject setObject:self.textView.text forKey:@"message"];
    AVUser * user = [AVUser currentUser];
       [self.myObject setObject:user.username forKey:@"userName"];
    
//    __block typeof(self) weakSelf = self;
//    self.map.block = ^(NSString * s){
//        
//        [weakSelf.myObject setObject:s forKey:@"location"];
//        [weakSelf.myObject save];
//        NSLog(@"s === %@",s);
//    };
    
    
    AVFile * attachment = [user objectForKey:@"image"];
    [self.myObject setObject:attachment forKey:@"userImage"];
    // 点赞 初始值0
    [self.myObject setObject:[NSNumber numberWithInt:0] forKey:@"praiseNumber"];

        // 数据保存
    [self.myObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        
    }];
    
}
- (void)tapAction:(id)sender
{
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:^{
        
    }];
    
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    
    UIImage * i = info[UIImagePickerControllerEditedImage];
    if (self.imageView.image == nil) {
        self.imageView.image = i;
        [self dismissViewControllerAnimated:YES completion:^{
        }];
        NSData * imageData = UIImagePNGRepresentation(self.imageView.image);
        AVFile * imageFile = [AVFile fileWithName:@"firstImage.png" data:imageData];
        self.button.enabled = NO;
        [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            
            [self.myObject setObject:imageFile forKey:@"firstImage"];
           
           
            self.button.enabled = YES;
        }];
            self.secondView.hidden = NO;
        
    }else if (self.secondView.image == nil){
        
        
        self.secondView.image = i;
        [self dismissViewControllerAnimated:YES completion:^{
        }];
        NSData * imageData = UIImagePNGRepresentation(self.secondView.image);
        AVFile * imageFile = [AVFile fileWithName:@"secondImage.png" data:imageData];
        self.button.enabled = NO;
        [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            
            [self.myObject setObject:imageFile forKey:@"secondImage"];
            self.button.enabled = YES;
        }];
        self.thirdView.hidden = NO;

    }else if (self.thirdView.image == nil){
        
        self.thirdView.image = i;
        [self dismissViewControllerAnimated:YES completion:^{
        }];
        NSData * imageData = UIImagePNGRepresentation(self.thirdView.image);
        AVFile * imageFile = [AVFile fileWithName:@"thirdImage.png" data:imageData];
        self.button.enabled = NO;
        [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            
            [self.myObject setObject:imageFile forKey:@"thirdImage"];
            self.button.enabled = YES;
        }];
        self.fourthView.hidden = NO;
  
    }else if (self.fourthView.image == nil){
        
        
        self.fourthView.image = i;
        [self dismissViewControllerAnimated:YES completion:^{
        }];
        NSData * imageData = UIImagePNGRepresentation(self.fourthView.image);
        AVFile * imageFile = [AVFile fileWithName:@"fourthImage.png" data:imageData];
        self.button.enabled = NO;
        [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            
            [self.myObject setObject:imageFile forKey:@"fourthImage"];
            self.button.enabled = YES;
        }];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
